//
//  GKZYUserLoginViewController.m
//  GKZYAid
//
//  Created by lanou3g on 16/3/14.
//  Copyright © 2016年 GKZYAid. All rights reserved.
//

#import "GKZYUserLoginViewController.h"

@interface GKZYUserLoginViewController ()

@end

@implementation GKZYUserLoginViewController

- (void)loadView
{
    self.loginView = [[GKZYUserLoginView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.view = self.loginView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"登陆";
    [self addBarButton];
    [self addForgetButtonAction];
    [self setDelegate];
    [self setThirdPartyButtonAction];
    self.loginView.userName.LTTextField.text = self.loginUserName;
}

//添加导航按钮
- (void)addBarButton
{
    [self.loginView.buttonLogin addTarget:self action:@selector(buttonLoginAction:) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithTitle:@"注册" style:UIBarButtonItemStylePlain target:self action:@selector(leftBarButtonAction:)];
    self.navigationItem.leftBarButtonItem = leftBarButton;
    
    UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(rightBarButtonAction:)];
    self.navigationItem.rightBarButtonItem = rightBarButton;
}

//获取用户信息
- (void)getUserMessage
{
    self.strUserName = self.loginView.userName.LTTextField.text;
    self.strUserPassword = self.loginView.userPassword.LTTextField.text;
}

//注册按钮点击事件
- (void)leftBarButtonAction:(UIBarButtonItem *)buttonRegister
{
    GKZYUserRegisterViewController *viewC = [[GKZYUserRegisterViewController alloc] init];
    [self.navigationController pushViewController:viewC animated:YES];
}

- (void)rightBarButtonAction:(UIBarButtonItem *)sender
{
    GKZYUserCenterViewController *userVC  = [[GKZYUserCenterViewController alloc] init];
    [self.navigationController pushViewController:userVC animated:YES];
}

//登陆按钮点击事件
- (void)buttonLoginAction:(UIButton *)buttonLogin
{
    [self textFieldResignFirstResponder];
    [self getUserMessage];
    if (self.strUserName.length && self.strUserPassword.length) {
        [AVUser logInWithUsernameInBackground:self.strUserName password:self.strUserPassword block:^(AVUser *user, NSError *error) {
            if (user) {
#warning ----- Succeed login will skip to PostBarView 登陆成功,将要跳转到贴吧页面
                //UserCenterViewController *userCenter = [[UserCenterViewController alloc] init];
                self.currentUserName = [AVUser currentUser].username;
                [UserMessageShare sharedUserMessage].currentUserName = [AVUser currentUser].username;
                [self.navigationController popViewControllerAnimated:YES];
                //[self.navigationController pushViewController:userCenter animated:YES];
            }else{
                if (error.code == 211) {
                    [self addUserIsNotExistsErrorAlertController];
                }
                if (error.code == 210) {
                    [self addUserPasswordWrongErrorAlertController];
                }
            }
        }];
    }else{
        [self addUserNameOrPasswordIsNilErrorAlertController];
    }
}

#pragma mark ---- 添加提示框 ----

//添加用户不存在提示框
- (void)addUserIsNotExistsErrorAlertController
{
    UIAlertController *alertContrller = [UIAlertController alertControllerWithTitle:@"用户不存在" message:@"请输入正确的用户名" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *sureAlertAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        self.loginView.userName.LTTextField.text = nil;
        self.loginView.userPassword.LTTextField.text = nil;
    }];
    [alertContrller addAction:sureAlertAction];
    [self presentViewController:alertContrller animated:YES completion:nil];
}

//添加用户密码错误提示框
- (void)addUserPasswordWrongErrorAlertController
{
    UIAlertController *alertContrller = [UIAlertController alertControllerWithTitle:@"密码不正确" message:@"" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *sureAlertAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        self.loginView.userPassword.LTTextField.text = nil;
    }];
    [alertContrller addAction:sureAlertAction];
    [self presentViewController:alertContrller animated:YES completion:nil];
}

//添加用户用户名密码为空提示框
- (void)addUserNameOrPasswordIsNilErrorAlertController
{
    UIAlertController *alertContrller = [UIAlertController alertControllerWithTitle:@"用户名密码不能为空" message:@"" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *sureAlertAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
    }];
    [alertContrller addAction:sureAlertAction];
    [self presentViewController:alertContrller animated:YES completion:nil];
}

//微信登录成功提示框
- (void)addWeChatLoginSucceedAlertControllerWithUserName:(NSString *)userName userIcon:(NSString *)userIcon
{
    UIAlertController *alertContrller = [UIAlertController alertControllerWithTitle:@"登陆成功" message:@"" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *sureAlertAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSMutableDictionary *dicMessage = [[NSMutableDictionary alloc] init];
        [dicMessage setValue:userName forKey:@"userName"];
        [dicMessage setValue:userIcon forKey:@"userIcon"];
        self.weChatUserMessageBlock(dicMessage);
        [AVUser logOut];
        [self.navigationController popViewControllerAnimated:YES];
    }];
    [alertContrller addAction:sureAlertAction];
    [self presentViewController:alertContrller animated:YES completion:nil];
}

#pragma mark ---- 忘记密码跳转事件 ----

- (void)addForgetButtonAction
{
    [self.loginView.buttonForgetPassword addTarget:self action:@selector(buttonForgetPasswordAciton:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)buttonForgetPasswordAciton:(UIButton *)sender
{
    GKZYUserForgetPasswordViewController *forgetPasswordVC  = [[GKZYUserForgetPasswordViewController alloc] init];
    [self.navigationController pushViewController:forgetPasswordVC animated:YES];
}

#pragma mark ---- 第三方登陆 ----

- (void)setThirdPartyButtonAction
{
    [self.loginView.buttonWeChat addTarget:self action:@selector(buttonWeChatAction:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)buttonWeChatAction:(UIButton *)sender
{
    [AVOSCloudSNS setupPlatform:AVOSCloudSNSWeiXin withAppKey:@"wxd930ea5d5a258f4f" andAppSecret:@"db426a9829e4b49a0dcac7b4162da6b6" andRedirectURI:@""];
    [AVOSCloudSNS loginWithCallback:^(id object, NSError *error) {
        if (error) {
            NSLog(@"%ld",(long)error.code);
        }else{
#warning ----- WeChat ThirdParty 微信第三方登陆
            [self addWeChatLoginSucceedAlertControllerWithUserName:object[@"username"] userIcon:object[@"avatar"]];
        }
    } toPlatform:AVOSCloudSNSWeiXin];
}


#pragma mark ---- textField代理事件 ----

//设置代理
- (void)setDelegate
{
    self.loginView.userName.LTTextField.delegate = self;
    self.loginView.userPassword.LTTextField.delegate = self;
}

//textField失去第一响应者
- (void)textFieldResignFirstResponder
{
    [self.loginView.userName.LTTextField resignFirstResponder];
    [self.loginView.userPassword.LTTextField resignFirstResponder];
}

//键盘回收
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
